package io.seata.sharding4.storage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.sharding4.common.dto.CommodityDTO;
import io.seata.sharding4.common.enums.RspStatusEnum;
import io.seata.sharding4.common.response.ObjectResponse;
import io.seata.sharding4.storage.entity.Storage;
import io.seata.sharding4.storage.mapper.StorageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存服务实现类
 *
 * @author Administrator
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {
    @Transactional
    @Override
    public ObjectResponse decreaseStorage(CommodityDTO commodityDTO) {
        QueryWrapper<Storage> queryWrapper = new QueryWrapper<Storage>().eq("commodity_code", commodityDTO.getCommodityCode());
        Storage tStorage = baseMapper.selectOne(queryWrapper);
        int storage = baseMapper.decreaseStorage(tStorage.getId(), commodityDTO.getCount());
        ObjectResponse<Object> response = new ObjectResponse<>();
        if (storage > 0) {
            response.setStatus(RspStatusEnum.SUCCESS.getCode());
            response.setMessage(RspStatusEnum.SUCCESS.getMessage());
            return response;
        }
        response.setStatus(RspStatusEnum.FAIL.getCode());
        response.setMessage(RspStatusEnum.FAIL.getMessage());
        return response;
    }
}
